import json
import os

from locust import HttpUser, TaskSet, task, between

class enterpriseManagement(TaskSet):
    ''''''
    @task
    def postAdd(self):
        header = {"Content-Type":"application/json;charset=utf-8"}
        url = "http://192.168.1.119:9002/sys/business/save"
        data = {
            "id": "102",
            "mobile": "14585131321",
            "username":"秦呆呆",
            "password":"10043453543",
        }
        response = self.client.post(url, data=json.dumps(data), headers=header, name="用户添加", catch_response=True)
        print(response.json())
        ''''''
        if res.json()["code"] == 10000:
            print("增加成功！")
            res.success()
        else:
            print("增加失败")
            res.failure("自定义失败：登陆失败")


    @task
    def getdel(self):
        url = "http://192.168.1.119:9002/sys/business/delete/{0}"
        self.client.get(url)
        with self.client.get(url=url, name="获取全部信息", catch_response=True) as res:
            # 下面断言，当我们返回的text中有项目管理则登录成功，反之失败
            if res.json()["code"] == 10000:
                print("删除成功！")
                res.success()
            else:
                print("删除失败")
                res.failure("获取失败")

    @task
    def getfindAll(self):
        url = "http://192.168.1.119:9002/sys/business/findAll"
        self.client.get(url)
        with self.client.get(url=url, name="获取全部信息", catch_response=True) as res:
            # 下面断言，当我们返回的text中有项目管理则登录成功，反之失败
            if res.json()["code"] == 10000:
                print("查询成功！")
                res.success()
            else:
                print("查询失败")
                res.failure("获取失败")

    @task
    def postmod(self):
        header = {"Content-Type": "application/json;charset=utf-8"}
        url = "http://192.168.1.119:9002/sys/business/update/{0}"
        data = {
            "id": "102",
            "mobile": "14585131321",
            "username": "秦呆呆",
            "password": "10043453543",
        }
        response = self.client.post(url, data=json.dumps(data), headers=header, name="用户添加", catch_response=True)
        print(response.json())
        ''''''
        if res.json()["code"] == 10000:
            print("修改成功！")
            res.success()
        else:
            print("修改失败")
            res.failure("自定义失败：登陆失败")


class WebsiteUser(HttpUser):
    tasks = [enterpriseManagement]
    min_wait = 1000
    max_wait = 5000


# 以下 便于当前脚本 本地调试
# 启动 当前脚本
if __name__ == "__main__":
    os.system("locust -f enterpriseManagementTest.py --web-host=127.0.0.1")



